package com.lst.servlet;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.entity.request.ClientMessageReqPara;
import com.lst.service.MstMessageService;
import com.lst.utils.RequestUtils;


/**
 * @ClassName: NoticeDetailServlet
 * @Description: 通知详情
 * @author zmm
 * @date 2018年03月17日下午14:03:41
 *
 */
public class NoticeDetailServlet extends BaseServlet {

    private static final long serialVersionUID = 1L;

    private MstMessageService mstMessageService = ctx.getBean(MstMessageService.class);
    
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) {
	doPost(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) {

	BaseResponse resPara = new BaseResponse();

	try {
	    ClientMessageReqPara reqPara = getReqPara(request);

	    if (isCheckField(reqPara, resPara)) {
	//	Integer clientid = Integer.valueOf(reqPara.getUserid());
		Integer msgId = Integer.valueOf(reqPara.getMsgId());

		mstMessageService.updateStatus(msgId); 
			
		resPara.setCode(CommCode.M_Y000000);
		resPara.setMessage(CommCode.M_Y000001);
		
	    }
	} catch (Exception e) {
	    resPara.setCode(CommCode.M_A000000);
	    resPara.setMessage(CommCode.M_SYS0001);

	    logger.error("doPost error: ", e);
	}

	try {
	    response.setContentType("application/json;charset=utf-8");
	    response.setHeader("pragma", "no-cache");
	    response.setHeader("cache-control", "no-cache");

	    Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss")
		    .excludeFieldsWithoutExposeAnnotation().serializeNulls()
		    .create();
	    String jsonResult = gson.toJson(resPara);
	//    logger.info("ClientMessageListServlet jsonResult: " + jsonResult);

	    PrintWriter out = response.getWriter();
	    out.print(jsonResult);
	    out.flush();
	    out.close();

//	    logger.info("doPost end run(s): "+ DateUtil.calLastedTime(startDate));
	} catch (Exception e) {
	    resPara.setCode(CommCode.M_A000000);
	    resPara.setMessage(CommCode.M_SYS0003);

	    logger.error("doPost error: ", e);
	}
    }

    @Override
    public ClientMessageReqPara getReqPara(HttpServletRequest request) {

	ClientMessageReqPara reqPara = null;

	try {
	    BaseRequest baseRequest = RequestUtils.getRequestPara(request,
		    new ClientMessageReqPara());

	    reqPara = (ClientMessageReqPara) baseRequest.clone();
	    String userid = request.getParameter("userid");
	    String msgId = request.getParameter("msgId");

	    reqPara.setUserid(userid);
        reqPara.setMsgId(msgId);

	} catch (Exception e) {
	    logger.error("getReqPara error: ", e);
	}
	return reqPara;
    }

    @Override
    public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
	boolean flag = false;

	ClientMessageReqPara cmReqPara = (ClientMessageReqPara) reqPara;
	BaseResponse cmResPara = (BaseResponse) resPara;

	if (StringUtils.isEmpty(cmReqPara.getUserid())) {
	    cmResPara.setCode(CommCode.M_A000000);
	    cmResPara.setMessage(CommCode.M_P000002);
	} else if (StringUtils.isEmpty(cmReqPara.getMsgId())) {
	    cmResPara.setCode(CommCode.M_A000000);
	    cmResPara.setMessage("通知id为空");
	} else {
	    flag = true;
	}
	return flag;
    }
}
